package com.personal.cuncunxing.module.home.recommend;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.personal.cuncunxing.R;
import com.personal.cuncunxing.module.home.pojo.Movie;

import java.util.List;


/**
 * Description:
 * Created by kode on 2020/5/26.
 */
public class RecommendRVAdapter extends RecyclerView.Adapter<RecommendRVAdapter.ViewHolder> {

    private String[] title = {"热门电影", "热播电视剧", "最新动漫"};
    private static Context context;
    private OnItemClick onItemClick;
    private RecommendSubRVAdapter recommendSubRVAdapter;
    private List<List<Movie>> listList;


    RecommendRVAdapter(Context context) {
        this.context = context;
    }


    public void updateUI() {
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_item_recommand, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        //设置适配器
        if (listList != null) {
            holder.recommendSubRVAdapter = new RecommendSubRVAdapter(context, listList.get(position));
        } else {
            holder.recommendSubRVAdapter = new RecommendSubRVAdapter(context, null);
        }
        recommendSubRVAdapter = holder.recommendSubRVAdapter;
        holder.recycler_view.setAdapter(holder.recommendSubRVAdapter);
        holder.title_tv.setText(title[position]);
        holder.find_more_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClick.onItemClick(position + 1);
            }
        });
    }

    @Override
    public int getItemCount() {
        return title.length;
    }

    public void setOnItemClick(OnItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }
    public void setOnSubItemClick(RecommendSubRVAdapter.OnItemClick onItemClick) {
        recommendSubRVAdapter.setOnSubItemClick(onItemClick);
    }
    public void setListList(List<List<Movie>> listList) {
        this.listList = listList;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        TextView title_tv;
        TextView find_more_tv;
        RecyclerView recycler_view;
        RecommendSubRVAdapter recommendSubRVAdapter;

        @SuppressLint("CutPasteId")
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            title_tv = itemView.findViewById(R.id.title_tv);
            find_more_tv = itemView.findViewById(R.id.find_more_tv);
            recycler_view = itemView.findViewById(R.id.recommend_sub_rv);
            //设置流布局，每行3列
            recycler_view.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL));
            //设置线性布局，默认垂直
//            LinearLayoutManager layoutManager = new LinearLayoutManager(context);
//            recycler_view.setLayoutManager(layoutManager);
        }
    }

    interface OnItemClick {
        void onItemClick(int position);
    }
}
